﻿using DongFangGuoKai.WMSPlatform.Commn.ApiResultModule;
using DongFangGuoKai.WMSPlatform.Commn.TokenModule;
using DongFangGuoKai.WMSPlatform.Commn.TokenModule.Models;
using DongFangGuoKai.WMSPlatform.Core;
using DongFangGuoKai.WMSPlatform.Domain.AGVModels;
using DongFangGuoKai.WMSPlatform.Domain.HZJLYModel;
using DongFangGuoKai.WMSPlatform.Domain.Models;
using DongFangGuoKai.WMSPlatform.Service.User;
using DongFangGuoKai.WMSPlatform.Service.User.DTO;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;

namespace DongFangGuoKai.WMSPlatform.WebApi.Controllers.BasicData
{
    /// <summary>
    /// 用户
    /// </summary>
    [Route("api/[controller]/[action]"), ApiController]
    public class UserController : ApiBaseController
    {
        private readonly IUserService userService;
        private readonly Repository<UserColors> usercolorrepost;
        private readonly IConfiguration configuration;

        /// <summary>
        /// 构造
        /// </summary>
        /// <param name="userService"></param>
        /// <param name="usercolorrepost"></param>
        /// <param name="configuration"></param>
        public UserController(
            IUserService userService,
            Repository<UserColors> usercolorrepost,
            IConfiguration configuration)
        {
            this.userService = userService;
            this.usercolorrepost = usercolorrepost;
            this.configuration = configuration;
        }

        /// <summary>
        /// 用户登录
        /// </summary>
        /// <param name="m"></param>
        /// <returns></returns>
        [HttpPost]
        public IActionResult UserLogin(LoginDto m)
        {
            if (string.IsNullOrEmpty(m.userName) || m.userName.Length == 0)
            {
                return Failed("账号不能为空");
            }
            if (string.IsNullOrEmpty(m.userPassword) || m.userPassword.Length == 0)
            {
                return Failed("密码不能为空");
            }
            if (string.IsNullOrEmpty(m.userName) || string.IsNullOrEmpty(m.userPassword))
            {
                return Failed("账号密码不能为空");
            }
            var obj = userService.GetLogin(m);
            if (obj == null)
            {
                return Failed("账号密码不对");
            }
            obj.Token = GetToken(obj.userId,obj.userName);      

            return Success(obj, "登录成功");
        }

        private string GetToken(int userId, string? userName)
        {
            var tokenModel = configuration.GetSection("Jwt").Get<JwtTokenModel>();
            tokenModel.UserId = userId;
            tokenModel.UserName = userName;
            return TokenHelper.CreateToken(tokenModel);

        }

        /// <summary>
        /// 用户选择颜色
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public IActionResult UserSelelctColor(SelectColorDTO selectColorDTO)
        {
            var data = userService.SelectColor(selectColorDTO, out string errMsg);
            if (data is not null)
            {
                return Success(data, errMsg);
            }
            return Failed(errMsg);

        }

        /// <summary>
        /// 用户移除颜色
        /// </summary>
        /// <param name="selectColorDTO"></param>
        /// <returns></returns>
        [HttpPost]
        public IActionResult RemoveUserColor(SelectColorDTO selectColorDTO)
        {

            var IsOk = userService.RemoveColor(selectColorDTO, out string errMsg);
            if (IsOk)
            {
                return Success(errMsg);
            }
            return Failed(errMsg);
        }
        /// <summary>
        /// 用户颜色查询
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public IActionResult GetAllUserColor()
        {
            var list = usercolorrepost.GetList(u => u.ColorState == 0 && u.ShowColor != 5 && u.ShowColor != 10);
            if (list is null)
            {
                return Failed("无数据");
            }
            return Success(list);
        }
        /// <summary>
        /// 添加用户
        /// </summary>
        /// <param name="m"></param>
        /// <returns></returns>
        [HttpPost]
        public IActionResult AddUser(UserRoleDTO m)
        {
            bool IsOK = userService.AddUser(m, out var errMsg);
            if (IsOK)
                return Success("添加成功！");
            return Failed(errMsg);
        }

        /// <summary>
        /// 获取用户
        /// </summary>
        /// <param name="uname">用户名</param>
        /// <param name="rid">角色Id</param>
        /// <param name="state">状态</param>
        /// <param name="pageIndex">当前页数</param>
        /// <param name="pageSize">每页显示条数</param>
        /// <returns></returns>
        [HttpGet]
        public IActionResult GetUserList(string? uname = "", int rid = 0, int state = 3, int pageIndex = 1, int pageSize = 5)
        {
            if (pageIndex <= 0 || pageSize <= 0)
            {
                return Success("分页条件有误");
            }
            int totalCount;
            var list = userService.GetUserList(uname, rid, state, pageIndex, pageSize, out totalCount);
            if (list != null)
            {
                return Success(list, totalCount);
            }
            return Failed("系统错误,获取失败");
        }
        /// <summary>
        /// 修改用户
        /// </summary>
        /// <param name="m"></param>
        /// <returns></returns>
        [HttpPut]
        public IActionResult UpdUser(UserRoleDTO m)
        {
            if (m.Id <= 0)
            {
                return Failed("id有误 不能小于0");
            }
            bool IsOK = userService.UpdUser(m,out string errMsg);
            if (IsOK)
            {
                return Success("修改成功");
            }
            return Failed(errMsg);
        }
        /// <summary>
        /// 删除用户
        /// </summary>
        /// <param name="uid"></param>
        /// <returns></returns>
        [HttpDelete]
        public IActionResult DelUser(int uid)
        {
            if (uid <= 0)
            {
                return Failed("Id有误不能小于等于0！");
            }
            bool IsOK = userService.DelUser(uid,out var errMsg);
            if (IsOK)
            {
                return Success("删除成功！");
            }
            return Failed(errMsg);
        }
        /// <summary>
        /// 根据主键获取用户信息
        /// </summary>
        /// <param name="uid"></param>
        /// <returns></returns>
        [HttpGet]
        public IActionResult URById(int uid)
        {
            if (uid <= 0)
            {
                return Failed("Id有误不能小于等于0！");
            }
            var obj = userService.URById(uid);
            if (obj != null)
            {
                return Success(obj);
            }
            return Failed("获取失败");
        }
    }
}
